package com.example.myapplication.video.video_sift;

import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

public class LinerLayout_Test extends LinearLayoutManager implements RecyclerView.OnChildAttachStateChangeListener {
    private PagerSnapHelper pagerSnapHelper;

    public LinerLayout_Test(Context context) {
        super(context);
    }

    @Override
    public void onAttachedToWindow(RecyclerView view) {

        super.onAttachedToWindow(view);
        if (pagerSnapHelper == null){
            pagerSnapHelper = new PagerSnapHelper();
            pagerSnapHelper.attachToRecyclerView(view);
        }
        view.addOnChildAttachStateChangeListener(this);
    }

    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);
       if (state == RecyclerView.SCROLL_STATE_IDLE){
           View snapView = pagerSnapHelper.findSnapView(this);
           if (onScrollItemListener!= null){
               onScrollItemListener.selected(getPosition(snapView),getPosition(snapView) == getItemCount()-1);
           }
       }
    }



    //接口回调方法i
    interface OnScrollItemListener{
        void selected(int postion,boolean isLoadMore);
        void release(int postion);
        void init (int postion);
    }
    //初始化接口
    private OnScrollItemListener onScrollItemListener;
    //给接口赋值
    public void setOnScrollItemListener(OnScrollItemListener onScrollItemListener) {
        this.onScrollItemListener = onScrollItemListener;
    }

    //绑定
    @Override
    public void onChildViewAttachedToWindow(@NonNull View view) {
        if (onScrollItemListener !=null ){
                onScrollItemListener.init(getPosition(view));
        }
    }
    //解绑
    @Override
    public void onChildViewDetachedFromWindow(@NonNull View view) {
        if (onScrollItemListener!= null){
            onScrollItemListener.release(getPosition(view));
        }
    }



}
